﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using NNS.Enumeration;
using NNS.Framework;
using NNS.UI;
using QLTB.BusinessLayer;
using QLTB.BusinessObjects;
using QLTB.Common;
using QLTB.DataAccess;
using QLTB.Nhom.UI;

namespace QLTB.NguoiDung.UI
{
    [System.ComponentModel.ToolboxItem(false)]
    public partial class ucDSNguoiDung : MyUIBase
    {
        private int _idnhom;
        private frmThemNguoiDung themNguoi;

        public ucDSNguoiDung()
        {
            InitializeComponent();
            chxAll.Enabled = MyLogin.Original.toantinh;
            Nap();
        }

        public int Idnhom
        {
            get { return _idnhom; }
            set
            {
                _idnhom = value;
                Nap();
            }
        }

        public event EventHandler Added;
        public event EventHandler<NhomEventArgs> Choosed;

        private void OnChoosed(NhomEventArgs e)
        {
            EventHandler<NhomEventArgs> choosed = Choosed;
            if (choosed != null) choosed(this, e);
        }


        private void OnAdded(EventArgs e)
        {
            EventHandler _handler = Added;
            if (_handler != null) _handler(this, e);
        }

        public void Nap()
        {
            if (!chxAll.Checked)
            {
                dsNguoiDung.TBL_nhanvien.Rows.Clear();
                SqlHelper.Helper.Fill(CommandType.Text,
                                      "SELECT     TBL_nhanvien.id, TBL_nhanvien.iddonvi, TBL_nhanvien.tenuser, TBL_nhanvien.matkhau, TBL_nhanvien.hoten, TBL_nhanvien.idchucdanh,TBL_nhanvien.idnhom, TBL_DONVI.TenDonVi FROM  TBL_nhanvien INNER JOIN TBL_DONVI ON TBL_nhanvien.iddonvi = TBL_DONVI.MaDonVi Where TBL_nhanvien.idnhom=@idnhom and TBL_nhanvien.iddonvi=@iddonvi ORDER BY TBL_nhanvien.iddonvi, TBL_nhanvien.tenuser",
                                      dsNguoiDung.TBL_nhanvien,
                                      new[]
                                          {
                                              new SqlParameter("@idnhom", Idnhom),
                                              new SqlParameter("iddonvi", MyLogin.Original.iddonvi),
                                          });
            }
            else
            {
                NapAll();
            }
        }

        public override void Add()
        {
            if (!TBL_quyenBL.isTaomoi(EnumComponent.iUser)) return;

            if (themNguoi == null)
            {
                themNguoi = new frmThemNguoiDung();
                themNguoi.Added += themNguoi_Added;
            }
            if (themNguoi.Nhanvien == null)
            {
                themNguoi.Nhanvien = new TBL_nhanvien
                                         {iddonvi = MyLogin.Original.iddonvi, idnhom = MyLogin.Original.idnhom};
            }
            themNguoi.Mode = EditMode.Add;
            themNguoi.ShowDialog();
        }

        private void themNguoi_Added(object sender, EventArgs e)
        {
            ///throw new NotImplementedException();
            Nap();
            OnAdded(null);
        }


        public void Deleted()
        {
            if (!TBL_quyenBL.isThaydoi(EnumComponent.iUser)) return;

            if (MyBox.ShowQuestion("Bạn thật sự muốn xóa không?", "Quản lý thiết bị") == DialogResult.No) return;

            if (gbList.SelectedRowsCount > 0)
            {
                gbList.DeleteSelectedRows();
            }
            else
            {
                gbList.DeleteRow(gbList.FocusedRowHandle);
            }

            Save();
            Nap();
        }

        public void Save()
        {
            DataTable dt = dsNguoiDung.TBL_nhanvien;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var da = new TBL_nhanvienDA();
                DataRow dr = dt.Rows[i];
                if (dr.RowState == DataRowState.Deleted)
                {
                    if (da.Delete((int) dr["id", DataRowVersion.Original]) < 0)
                    {
                        ShowWarning(SqlHelper.Helper.LastError.Message, "Quản lý người dùng.");
                    }
                }
            }
        }

        private void gbList_RowCellClick(object sender, RowCellClickEventArgs e)
        {
        }

        private void gbList_DoubleClick(object sender, EventArgs e)
        {
            if (!TBL_quyenBL.isThaydoi(EnumComponent.iUser)) return;

            object _ob = gbList.GetFocusedRowCellValue("id");
            if (_ob == null) return;
            var da = new TBL_nhanvienDA();
            TBL_nhanvien nv = da.Get((int) _ob);
            if (nv != null)
            {
                if (themNguoi == null)
                {
                    themNguoi = new frmThemNguoiDung();
                    themNguoi.Added += themNguoi_Added;
                }
                themNguoi.Nhanvien = nv;
                themNguoi.Mode = EditMode.Change;
                themNguoi.ShowDialog();
            }
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                gbList_DoubleClick(null, null);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                Deleted();
                Save();
                Nap();
            }
        }


        public void NapAll()
        {
            dsNguoiDung.TBL_nhanvien.Rows.Clear();
            SqlHelper.Helper.Fill(CommandType.Text,
                                  "SELECT     TBL_nhanvien.id, TBL_nhanvien.iddonvi, TBL_nhanvien.tenuser, TBL_nhanvien.matkhau, TBL_nhanvien.hoten, TBL_nhanvien.idchucdanh,TBL_nhanvien.idnhom, TBL_DONVI.TenDonVi FROM  TBL_nhanvien INNER JOIN TBL_DONVI ON TBL_nhanvien.iddonvi = TBL_DONVI.MaDonVi where TBL_nhanvien.idnhom=@idnhom ORDER BY TBL_nhanvien.iddonvi, TBL_nhanvien.tenuser",
                                  dsNguoiDung.TBL_nhanvien, new[] {new SqlParameter("@idnhom", Idnhom),});
        }

        private void ucDSNguoiDung_MouseHover(object sender, EventArgs e)
        {
        }

        private void gbList_Click(object sender, EventArgs e)
        {
            OnChoosed(new NhomEventArgs {Id = 2});
        }

        private void chxAll_CheckedChanged(object sender, EventArgs e)
        {
            Enabled = false;
            Nap();
            Enabled = true;
        }
    }
}